package gh2;



import deque.Deque;
import deque.LinkedListDeque;


//Note: This file will not compile until you complete the Deque implementations
public class GuitarString {
    /**
     * Constants. Do not change. In case you're curious, the keyword final
     * means the values cannot be changed at runtime. We'll discuss this and
     * other topics in lecture on Friday.
     */
    private static final int SR = 44100;      // Sampling Rate
    private static final double DECAY = .996; // energy decay factor

    /* Buffer for storing sound data. */

    private Deque<Double> buffer;

    /* Create a guitar string of the given frequency.  */
    public GuitarString(double frequency) {

        this.buffer = new LinkedListDeque<Double>();
        long result = Math.round(SR / frequency);
        System.out.println(result);
        while (result > 0) {
            buffer.addFirst(0.0);
            result = result - 1;
        }
    }



    /* Pluck the guitar string by replacing the buffer with white noise. */
    public void pluck() {
        int buffer_size = buffer.size();
        this.buffer = new LinkedListDeque<Double>();
        while (buffer_size > 0) {
            double r = Math.random() - 0.5;
            buffer.addFirst(r);
            buffer_size--;
        }

    }


    public void tic() {
        double result = 0.5 * (buffer.get(0) + buffer.get(1)) * DECAY;
        System.out.println(result);
        buffer.removeFirst();
        buffer.addLast(result);


    }

    /* Return the double at the front of the buffer. */
    public double sample() {
        return buffer.get(0);
    }
}

